package Thread;
//synchronized  的同步代码块   ，   同步方法小例


class MySynchronized_1 implements Runnable {
    private int ticket = 10000;
    @Override
    public void run() {

        while (ticket > 0)
    //锁住类的实例对象
            synchronized (this) {
            try {
                Thread.sleep(500);
            }catch(Exception e) {
                e.printStackTrace();
            }
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() +
                            "剩余" + --ticket);
                }
            }
    }

//    public void run () {
//        while (ticket > 0){
//            //锁住类对象
//
//        synchronized(MySynchronized_1.class) {
//            if (ticket > 0)
//                System.out.println(Thread.currentThread().getName() +
//                        "剩余" + --ticket);
//        }
//    }

//        public void run () {
//        while (ticket > 0)
//            this.fun();
//    }
    //锁住类的对象
//    public static synchronized void fun () {
//        if (ticket > 0)
//            System.out.println(Thread.currentThread().getName() +
//                    "剩余" + --ticket);
//    }


//        public void run () {
//        while (ticket > 0)
//            this.fun();
//    }
    //锁住类的实例对象
//    public synchronized void fun () {
//        if (ticket > 0)
//            System.out.println(Thread.currentThread().getName() +
//                    "剩余" + --ticket);
//    }
}
public class MySynchronized {
    public static void main(String[] args) {
        MySynchronized_1 mt = new MySynchronized_1();
        Thread mt1 = new Thread(mt,"A");
        Thread mt2 = new Thread(mt,"B");
        Thread mt3 = new Thread(mt,"C");
        mt1.start();
        mt2.start();
        mt3.start();

    }
}

